﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;

public partial class ClassStandard : System.Web.UI.Page
{
    ILE_Config ileConfig = new ILE_Config();
    ILE_Global ileGlobal = new ILE_Global();

    ClassBLL classLogic = new ClassBLL();
    ClassStandardsBLL classStandardsLogic = new ClassStandardsBLL();

    protected int CourseID
    {
        get
        {
            return (int)ViewState["CourseID"];
        }
        set
        {
            ViewState["CourseID"] = value;
        }
    }

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
        {
            GridViewClassList.Sort("Title", SortDirection.Ascending);
        }
    }

    protected void GridViewClassList_SelectedIndexChanged(object sender, EventArgs e)
    {
        if (GridViewClassList.SelectedIndex != -1)
        {
            CourseID = Convert.ToInt32(GridViewClassList.SelectedValue.ToString());

            ResetStandardsConfiguration();
            LoadStandardsConfiguration(CourseID);


            // Fill 'Edit Course Section Standards' ddlist Sections
            {
                lblDdlistSectionWarning.Visible = false;
                

                Classes.Courses_SectionDataTable sections = classLogic.GetSectionsByCourseID(CourseID, false);
                ddlistEditSectionSections.Items.Clear();
                ddlistEditSectionSections.Items.Add(new ListItem("-- Select Section --", "0"));
                foreach (Classes.Courses_SectionRow section in sections)
                {
                    if (section.IsEnabled == true)
                        ddlistEditSectionSections.Items.Add(new ListItem(section.SectionNumber.ToString(), section.SectionNumber.ToString()));
                    else
                        lblDdlistSectionWarning.Visible = true;
                }

                PanelSectionStandard.Visible = false;

                sections.Dispose();
            }

            // Display sections using its own settings...
            {
                lblSectionUsingOwnSetting.Text = "";
                PanelSectionUsingOwnSetting.Visible = false;

                foreach (ListItem item in ddlistEditSectionSections.Items)
                {
                    int tempSectionNumber = Convert.ToInt32(item.Value);
                    ClassStandards.Courses_Standard_SectionRow sectionStandard = classStandardsLogic.GetClassSectionStandard(CourseID, tempSectionNumber, 1);
                    if (sectionStandard != null && sectionStandard.UseSectionInfo)
                    {
                        lblSectionUsingOwnSetting.Text += sectionStandard.SectionNumber.ToString() + " ";
                        PanelSectionUsingOwnSetting.Visible = true;
                    }
                }
            }

            {
                Classes.CoursesRow course = classLogic.GetCourse(CourseID);
                if (course.IsEnabled == false)
                {
                    // *SteveMsg
                    string _msg = "Warning! This class is <u><b>NOT</b></u> enabled.<br />Remember to enable the class!";
                    ModalPopupMessage.ShowModalPopup("ILE Message", _msg, 2);
                }
            }

            TabContainerClassStandards.Visible = true;
        }
        else
        {
            TabContainerClassStandards.Visible = false;
        }
    }

    /// <summary>
    /// When Course is Selected, Load Settings from Database and Update the items.
    /// </summary>
    /// <param name="CourseID"></param>
    private void LoadStandardsConfiguration(int CourseID)
    {
        for (int StandardID = 1; StandardID <= 4; StandardID++)
        {
            if (!classStandardsLogic.IsDuplicated(CourseID, StandardID))
            {
                classStandardsLogic.InsertClassStandard(CourseID, StandardID, true);
            }
        }

        ClassStandards.Courses_StandardDataTable courseStandards =
            classStandardsLogic.GetClassStandardsByCourseID(CourseID);

        foreach (ClassStandards.Courses_StandardRow row in courseStandards)
        {
            switch (row.StandardID)
            {
                case 1:
                    LoadStandardEnabled(CourseID, 1, row.IsStandardEnabled);
                    LoadStandardIntroduction(CourseID, 1, row);
                    LoadLearningObjects(CourseID, 1);
                    break;
                case 2:
                    LoadStandardEnabled(CourseID, 2, row.IsStandardEnabled);
                    LoadStandardIntroduction(CourseID, 2, row);
                    LoadLearningObjects(CourseID, 2);
                    break;
                case 3:
                    LoadStandardEnabled(CourseID, 3, row.IsStandardEnabled);
                    LoadStandardIntroduction(CourseID, 3, row);
                    LoadLearningObjects(CourseID, 3);
                    break;
                case 4:
                    LoadStandardEnabled(CourseID, 4, row.IsStandardEnabled);
                    LoadStandardIntroduction(CourseID, 4, row);
                    LoadLearningObjects(CourseID, 4);
                    break;
            }
        }
    }

    private void LoadStandardEnabled(int CourseID, int StandardID, bool IsStandardEnabled)
    {
        if (IsStandardEnabled)
        {
            switch (StandardID)
            {
                case 1:
                    chkboxStandardEnabledNeeded.Checked = true;
                    break;
                case 2:
                    chkboxStandardEnabledAccessing.Checked = true;
                    break;
                case 3:
                    chkboxStandardEnabledEvaluating.Checked = true;
                    break;
                case 4:
                    chkboxStandardEnabledUsing.Checked = true;
                    break;
            }
        }
    }

    private void LoadStandardIntroduction(int CourseID, int StandardID, ClassStandards.Courses_StandardRow row)
    {
        switch (StandardID)
        {
            case 1:
                lnkbtnStandardIntroductionNeeded.Text = row.IntroTitle;
                if (!row.IsIntroEnabled)
                    lnkbtnStandardIntroductionNeeded.ForeColor = System.Drawing.Color.Gray;
                else
                    lnkbtnStandardIntroductionNeeded.ForeColor = System.Drawing.Color.Empty;
                break;
            case 2:
                lnkbtnStandardIntroductionAccessing.Text = row.IntroTitle;
                if (!row.IsIntroEnabled)
                    lnkbtnStandardIntroductionAccessing.ForeColor = System.Drawing.Color.Gray;
                else
                    lnkbtnStandardIntroductionAccessing.ForeColor = System.Drawing.Color.Empty;
                break;
            case 3:
                lnkbtnStandardIntroductionEvaluating.Text = row.IntroTitle;
                if (!row.IsIntroEnabled)
                    lnkbtnStandardIntroductionEvaluating.ForeColor = System.Drawing.Color.Gray;
                else
                    lnkbtnStandardIntroductionEvaluating.ForeColor = System.Drawing.Color.Empty;
                break;
            case 4:
                lnkbtnStandardIntroductionUsing.Text = row.IntroTitle;
                if (!row.IsIntroEnabled)
                    lnkbtnStandardIntroductionUsing.ForeColor = System.Drawing.Color.Gray;
                else
                    lnkbtnStandardIntroductionUsing.ForeColor = System.Drawing.Color.Empty;
                break;
        }
    }

    private void LoadLearningObjects(int CourseID, int StandardID)
    {
        ClassStandards.Courses_LearnObjDataTable learnObjs = classStandardsLogic.GetClassLearnObjs(CourseID, StandardID);

        Repeater repeaterLearningObj = new Repeater();
        switch (StandardID)
        {
            case 1:
                repeaterLearningObj = RepeaterLearningObjNeeded;
                break;
            case 2:
                repeaterLearningObj = RepeaterLearningObjAccessing;
                break;
            case 3:
                repeaterLearningObj = RepeaterLearningObjEvaluating;
                break;
            case 4:
                repeaterLearningObj = RepeaterLearningObjUsing;
                break;
        }
        repeaterLearningObj.DataSource = learnObjs.Rows;
        repeaterLearningObj.DataBind();
    }

    private void ResetStandardsConfiguration()
    {
        AccordionStandards.SelectedIndex = -1;
        chkboxStandardEnabledNeeded.Checked = false;
        chkboxStandardEnabledAccessing.Checked = false;
        chkboxStandardEnabledEvaluating.Checked = false;
        chkboxStandardEnabledUsing.Checked = false;
    }

    protected void chkboxStandardEnabled_CheckedChanged(object sender, EventArgs e)
    {
        //int CourseID = Convert.ToInt32(GridViewClassList.SelectedValue.ToString());
        int StandardID = Convert.ToInt32(AccordionStandards.SelectedIndex) + 1;

        if (((CheckBox)sender).Checked)
        {
            ClassStandards.Courses_StandardRow classStandard = classStandardsLogic.GetClassStandard(CourseID, StandardID);
            classStandardsLogic.UpdateClassStandard(classStandard.IntroTitle, classStandard.IntroContent, classStandard.IsIntroEnabled, true, CourseID, StandardID);
        }
        else
        {
            ClassStandards.Courses_StandardRow classStandard = classStandardsLogic.GetClassStandard(CourseID, StandardID);
            classStandardsLogic.UpdateClassStandard(classStandard.IntroTitle, classStandard.IntroContent, classStandard.IsIntroEnabled, false, CourseID, StandardID);
        }
    }

    protected void lnkbtnStandardIntroduction_Click(object sender, CommandEventArgs e)
    {
        int StandardId = Convert.ToInt32(e.CommandArgument.ToString());
        //int CourseID = Convert.ToInt32(GridViewClassList.SelectedValue.ToString());

        switch (StandardId)
        {
            case 1:
                lblStandardIntroductionTitle.Text = "Getting Started";
                break;
            case 2:
                lblStandardIntroductionTitle.Text = "Finding Sources";
                break;
            case 3:
                lblStandardIntroductionTitle.Text = "Evaluating Sources";
                break;
            case 4:
                lblStandardIntroductionTitle.Text = "Writing with Sources";
                break;
        }

        btnStandardIntroductionOk.CommandArgument = StandardId.ToString();

        if (!classStandardsLogic.IsDuplicated(CourseID, StandardId))
        {
            classStandardsLogic.InsertClassStandard(CourseID, StandardId, true);
        }

        ClassStandards.Courses_StandardRow classStandard = classStandardsLogic.GetClassStandard(CourseID, StandardId);
        chkboxStandardIntroductionEnabled.Checked = classStandard.IsIntroEnabled;
        txtStandardIntroductionTitle.Text = classStandard.IntroTitle;
        txtStandardIntroduction.Value = classStandard.IntroContent;

        ModalPopupExtenderStandardIntroduction.Show();
    }

    protected void btnStandardIntroductionOk_Click(object sender, CommandEventArgs e)
    {
        int StandardId = Convert.ToInt32(e.CommandArgument.ToString());
        //int CourseID = Convert.ToInt32(GridViewClassList.SelectedValue.ToString());

        string introTitle = txtStandardIntroductionTitle.Text.ToString();
        string introDescription = txtStandardIntroduction.Value.ToString();
        bool introEnabled = chkboxStandardIntroductionEnabled.Checked;

        bool standardEnabled = classStandardsLogic.GetClassStandard(CourseID, StandardId).IsStandardEnabled;

        bool result = classStandardsLogic.UpdateClassStandard(introTitle, introDescription, introEnabled, standardEnabled, CourseID, StandardId);

        if (result)
        {
            LoadStandardsConfiguration(CourseID);
            // *SteveMsg
            string _msg = "Standard Introduction has been successfully updated.";
            ModalPopupMessage.ShowModalPopup("ILE Message", _msg, 1);
        }
        else
        {
            // *SteveMsg
            string _msg = "Failed to update Standard Introduction.";
            ModalPopupMessage.ShowModalPopup("ILE Message", _msg, 2);
        }
    }

    protected void RepeaterLearningObj_ItemDataBound(object sender, RepeaterItemEventArgs e)
    {
        if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
        {
            ClassStandards.Courses_LearnObjRow row = e.Item.DataItem as ClassStandards.Courses_LearnObjRow;

            ImageButton SortUp = e.Item.FindControl("imgbtnSortUp") as ImageButton;
            ImageButton SortDown = e.Item.FindControl("imgbtnSortDown") as ImageButton;
            LinkButton Title = e.Item.FindControl("lnkbtnEditLearningObj") as LinkButton;
            LinkButton Remove = e.Item.FindControl("lnkbtnLearningObjRemove") as LinkButton;

            // Passes Two Command Arguments. StandardID;LearnObjID
            SortUp.CommandArgument = SortDown.CommandArgument = Remove.CommandArgument = row.StandardID.ToString() + ";" + row.LearnObjID.ToString();

            if (!row.UseAlterInfo)
            {
                LearnObjsBLL learnObjsLogic = new LearnObjsBLL();
                LearnObjs.LearnObjsRow learnObj = learnObjsLogic.GetLearnObj(row.LearnObjID);
                Title.Text = learnObj.Title;
            }
            else
            {
                Title.Text = row.AlterTitle;
            }

            int StandardID = Convert.ToInt16(Title.CommandArgument);

            switch (StandardID)
            {
                case 1:
                    Title.CommandArgument = "1;" + row.LearnObjID.ToString();
                    break;
                case 2:
                    Title.CommandArgument = "2;" + row.LearnObjID.ToString();
                    break;
                case 3:
                    Title.CommandArgument = "3;" + row.LearnObjID.ToString();
                    break;
                case 4:
                    Title.CommandArgument = "4;" + row.LearnObjID.ToString();
                    break;
                default:
                    break;
            }

            if (!row.IsEnabled)
            {
                Title.ForeColor = System.Drawing.Color.Gray;
            }
        }
    }

    protected void imgbtnSort_Click(object sender, CommandEventArgs e)
    {
        //int CourseID = Convert.ToInt32(GridViewClassList.SelectedValue.ToString());
        string[] commandArguments = new string[2];
        commandArguments = e.CommandArgument.ToString().Split(';');
        int StandardID = Convert.ToInt32(commandArguments[0]);
        int LearnObjID = Convert.ToInt32(commandArguments[1]);

        switch (e.CommandName)
        {
            case "SortUp":
                classStandardsLogic.DecreaseClassLearnObjSortOrder(CourseID, StandardID, LearnObjID);
                break;
            case "SortDown":
                classStandardsLogic.IncreaseClassLearnObjSortOrder(CourseID, StandardID, LearnObjID);
                break;
            default:
                break;
        }

        LoadLearningObjects(CourseID, StandardID);
    }

    protected void lnkbtnLearningObjRemove_Click(object sender, CommandEventArgs e)
    {
        //int CourseID = Convert.ToInt32(GridViewClassList.SelectedValue.ToString());
        string[] commandArguments = new string[2];
        commandArguments = e.CommandArgument.ToString().Split(';');
        int StandardID = Convert.ToInt32(commandArguments[0]);
        int LearnObjID = Convert.ToInt32(commandArguments[1]);

        bool result = classStandardsLogic.DeleteClassLearnObj(CourseID, StandardID, LearnObjID);

        if (result)
        {
            LoadStandardsConfiguration(CourseID);
            // *SteveMsg
            string _msg = "The Learning Object has been Successfully Deleted.";
            ModalPopupMessage.ShowModalPopup("ILE Message", _msg, 1);
        }
        else
        {
            // *SteveMsg
            string _msg = "Failed to Delete the Learning Object.";
            ModalPopupMessage.ShowModalPopup("ILE Message", _msg, 2);
        }
    }

    /// <summary>
    /// Edit Existing Learning Object
    /// </summary>
    protected void lnkbtnEditLearningObj_Click(object sender, CommandEventArgs e)
    {
        //int CourseID = Convert.ToInt32(GridViewClassList.SelectedValue.ToString());
        string[] commandArguments = new string[2];
        commandArguments = e.CommandArgument.ToString().Split(';');
        int StandardID = Convert.ToInt32(commandArguments[0]);
        int LearnObjID = Convert.ToInt32(commandArguments[1]);

        ClassStandards.Courses_LearnObjRow classLearnObj = classStandardsLogic.GetClassLearnObj(CourseID, StandardID, LearnObjID);

        chkboxEditUseAlternativeLearningObjDirections.Checked = classLearnObj.UseAlterInfo;
        if (classLearnObj.UseAlterInfo)
        {
            lblEditDefaultLearningObjDirections.Visible = false;
            txtEditAlterLearningObjDirections.Visible = true;
            txtEditAlterLearningObjTitle.Enabled = true;

            txtEditAlterLearningObjTitle.Text = classLearnObj.AlterTitle;
            txtEditAlterLearningObjDirections.Value = classLearnObj.AlterDescription;
            chkboxEditLearningObjEnabled.Checked = classLearnObj.IsEnabled;
        }
        else
        {
            lblEditDefaultLearningObjDirections.Visible = true;
            txtEditAlterLearningObjDirections.Visible = false;
            txtEditAlterLearningObjTitle.Enabled = false;

            LearnObjsBLL learnObjsLogic = new LearnObjsBLL();
            LearnObjs.LearnObjsRow learnObj = learnObjsLogic.GetLearnObj(LearnObjID);

            txtEditAlterLearningObjTitle.Text = learnObj.Title;
            lblEditDefaultLearningObjDirections.Text = learnObj.Instruction;
            chkboxEditLearningObjEnabled.Checked = classLearnObj.IsEnabled;
        }

        btnEditLearningObjOk.CommandArgument = e.CommandArgument.ToString();

        ModalPopupExtenderEditLearningObj.Show();
    }

    protected void lnkbtnLearningObjAdd_Click(object sender, CommandEventArgs e)
    {
        //int CourseID = Convert.ToInt32(GridViewClassList.SelectedValue.ToString());
        int StandardId = Convert.ToInt32(e.CommandArgument.ToString());

        chkboxAddUseAlternativeLearningObjDirections.Checked = false;

        btnAddLearningObjOk.CommandArgument = StandardId.ToString();

        switch (StandardId)
        {
            case 1:
                lblAddLearningObjTitle.Text = "Getting Started";
                break;
            case 2:
                lblAddLearningObjTitle.Text = "Finding Sources";
                break;
            case 3:
                lblAddLearningObjTitle.Text = "Evaluating Sources";
                break;
            case 4:
                lblAddLearningObjTitle.Text = "Writing with Sources";
                break;
        }

        // Fill ddlistAvailableLearningObj
        LearnObjsBLL learnObjsLogic = new LearnObjsBLL();
        LearnObjs.RelationsLearnObjStandardDataTable learnObjs = learnObjsLogic.GetLearnObjsByStandardID(StandardId);
        List<LearnObjs.RelationsLearnObjStandardRow> sortedLearnObjs = new List<LearnObjs.RelationsLearnObjStandardRow>();
        ddlistAvailableLearningObj.Items.Clear();

        ClassStandards.Courses_LearnObjDataTable existingLearnObjs = classStandardsLogic.GetClassLearnObjs(CourseID, StandardId);

        int tempIterator = (learnObjs.Count - 1);

        for (int i = tempIterator; i >= 0; i--)
        {
            foreach (ClassStandards.Courses_LearnObjRow existingLearnObj in existingLearnObjs)
            {
                if (learnObjs[i].LearnObjID == existingLearnObj.LearnObjID)
                {
                    learnObjs.Rows.RemoveAt(i);
                    break;
                }
            }
        }

        if (learnObjs.Count == 0)
        {
            ListItem defaultItem = new ListItem("--- No Learning Object Available ---", "0");
            ddlistAvailableLearningObj.Items.Add(defaultItem);
        }
        else
        {
            ListItem defaultItem = new ListItem("--- Select Learning Object ---", "0");
            ddlistAvailableLearningObj.Items.Add(defaultItem);
            sortedLearnObjs = learnObjs.OrderBy(p => p.Title).ToList();
        }

        foreach (LearnObjs.RelationsLearnObjStandardRow learnObj in sortedLearnObjs)
        {
            ListItem newItem = new ListItem(learnObj.Title, learnObj.LearnObjID.ToString());
            ddlistAvailableLearningObj.Items.Add(newItem);
        }

        txtAddAlterLearningObjDirections.Visible = false;
        lblAddDefaultLearningObjDirections.Visible = true;
        lblAddDefaultLearningObjDirections.Text = "<span style='width: 100%; text-align: center; font-weight: bold;'>Please Select a Learning Object.</span>";

        ModalPopupExtenderAddLearningObj.Show();
    }

    protected void ddlistAvailableLearningObj_SelectedIndexChanged(object sender, EventArgs e)
    {
        LearnObjsBLL learnObjsLogic = new LearnObjsBLL();

        if (ddlistAvailableLearningObj.SelectedIndex != 0)
        {
            int LearnObjID = Convert.ToInt32(ddlistAvailableLearningObj.SelectedValue);
            LearnObjs.LearnObjsRow learnObj = learnObjsLogic.GetLearnObj(LearnObjID);
            txtAddAlterLearningObjTitle.Text = learnObj.Title;
            lblAddDefaultLearningObjDirections.Text = learnObj.Instruction;
        }
        else
        {
            chkboxAddUseAlternativeLearningObjDirections.Checked = false;
            txtAddAlterLearningObjTitle.Text = String.Empty;
            txtAddAlterLearningObjDirections.Value = String.Empty;
            txtAddAlterLearningObjTitle.Enabled = false;
            txtAddAlterLearningObjDirections.Visible = false;
            lblAddDefaultLearningObjDirections.Visible = true;
            lblAddDefaultLearningObjDirections.Text = "<span style='width: 100%; text-align: center; font-weight: bold;'>Please Select a Learning Object.</span>";
        }

        ModalPopupExtenderAddLearningObj.Show();
    }

    protected void chkboxAddUseAlternativeLearningObjDirections_CheckedChanged(object sender, EventArgs e)
    {
        if (chkboxAddUseAlternativeLearningObjDirections.Checked)
        {
            txtAddAlterLearningObjTitle.Enabled = true;
            txtAddAlterLearningObjDirections.Visible = true;
            lblAddDefaultLearningObjDirections.Visible = false;
        }
        else
        {
            txtAddAlterLearningObjTitle.Enabled = false;
            txtAddAlterLearningObjDirections.Visible = false;
            lblAddDefaultLearningObjDirections.Visible = true;
        }

        ModalPopupExtenderAddLearningObj.Show();
    }

    protected void btnAddLearningObjOk_Click(object sender, CommandEventArgs e)
    {
        if (ddlistAvailableLearningObj.SelectedIndex != 0)
        {
            int StandardId = Convert.ToInt32(e.CommandArgument.ToString());
            int LearnObjID = Convert.ToInt32(ddlistAvailableLearningObj.SelectedValue);
            string AlterTitle = txtAddAlterLearningObjTitle.Text;
            string AlterDirections = txtAddAlterLearningObjDirections.Value;
            bool IsEnabled = chkboxAddLearningObjEnabled.Checked;
            bool result = false;

            if (!classStandardsLogic.IsClassLearnObjDuplicated(CourseID, StandardId, LearnObjID))
            {
                if (chkboxAddUseAlternativeLearningObjDirections.Checked)
                {
                    result = classStandardsLogic.InsertClassLearnObj(CourseID, StandardId, LearnObjID, true, AlterTitle, AlterDirections, IsEnabled);
                }
                else
                {
                    result = classStandardsLogic.InsertClassLearnObj(CourseID, StandardId, LearnObjID, false, String.Empty, String.Empty, IsEnabled);
                }

                if (result)
                {
                    LoadLearningObjects(CourseID, StandardId);
                    ResetAddNewLearningObj();
                    // *SteveMsg
                    string _msg = "New Learning Object has been successfully added.";
                    ModalPopupMessage.ShowModalPopup("ILE Message", _msg, 1);
                }
                else
                {
                    ResetAddNewLearningObj();
                    // *SteveMsg
                    string _msg = "Failed to Add a New Learning Object.";
                    ModalPopupMessage.ShowModalPopup("ILE Message", _msg, 2);
                }
            }
            else
            {
                ResetAddNewLearningObj();
                // *SteveMsg
                string _msg = "The Learning Object has already been added.";
                ModalPopupMessage.ShowModalPopup("ILE Message", _msg, 2);
            }
        }
    }

    protected void ResetAddNewLearningObj()
    {
        chkboxAddUseAlternativeLearningObjDirections.Checked = false;
        txtAddAlterLearningObjTitle.Text = String.Empty;
        txtAddAlterLearningObjTitle.Enabled = false;
        txtAddAlterLearningObjDirections.Visible = false;
        lblAddDefaultLearningObjDirections.Text = "<span style='width: 100%; text-align: center; font-weight: bold;'>Please Select a Learning Object.</span>";
        lblAddDefaultLearningObjDirections.Visible = true;
        chkboxAddLearningObjEnabled.Checked = true;
    }

    protected void chkboxEditUseAlternativeLearningObjDirections_CheckedChanged(object sender, EventArgs e)
    {
        //int CourseID = Convert.ToInt32(GridViewClassList.SelectedValue.ToString());
        string[] commandArguments = new string[2];
        commandArguments = btnEditLearningObjOk.CommandArgument.ToString().Split(';');
        int StandardID = Convert.ToInt32(commandArguments[0]);
        int LearnObjID = Convert.ToInt32(commandArguments[1]);

        ClassStandards.Courses_LearnObjRow classLearnObj = classStandardsLogic.GetClassLearnObj(CourseID, StandardID, LearnObjID);

        if (chkboxEditUseAlternativeLearningObjDirections.Checked)
        {
            lblEditDefaultLearningObjDirections.Visible = false;
            txtEditAlterLearningObjDirections.Visible = true;
            txtEditAlterLearningObjTitle.Text = String.Empty;
            txtEditAlterLearningObjTitle.Enabled = true;
        }
        else
        {
            lblEditDefaultLearningObjDirections.Visible = true;
            txtEditAlterLearningObjDirections.Value = String.Empty;
            txtEditAlterLearningObjDirections.Visible = false;
            txtEditAlterLearningObjTitle.Enabled = false;

            LearnObjsBLL learnObjsLogic = new LearnObjsBLL();
            LearnObjs.LearnObjsRow learnObj = learnObjsLogic.GetLearnObj(LearnObjID);

            txtEditAlterLearningObjTitle.Text = learnObj.Title;
        }

        ModalPopupExtenderEditLearningObj.Show();
    }

    protected void btnEditLearningObjOk_Click(object sender, CommandEventArgs e)
    {
        //int CourseID = Convert.ToInt32(GridViewClassList.SelectedValue.ToString());
        string[] commandArguments = new string[2];
        commandArguments = e.CommandArgument.ToString().Split(';');
        int StandardID = Convert.ToInt32(commandArguments[0]);
        int LearnObjID = Convert.ToInt32(commandArguments[1]);

        bool UseAlterInfo = chkboxEditUseAlternativeLearningObjDirections.Checked;

        string AlterTitle = String.Empty;
        string AlterDirections = String.Empty;

        if (UseAlterInfo)
        {
            AlterTitle = txtEditAlterLearningObjTitle.Text;
            AlterDirections = txtEditAlterLearningObjDirections.Value;
        }
        else
        {
            AlterTitle = String.Empty;
            AlterDirections = String.Empty;
        }

        bool IsEnabled = chkboxEditLearningObjEnabled.Checked;

        bool result = classStandardsLogic.UpdateClassLearnObj(UseAlterInfo, AlterTitle, AlterDirections, IsEnabled, CourseID, StandardID, LearnObjID);

        if (result)
        {
            // *SteveMsg
            string _msg = "The Learning Object has been successfully edited.";
            ModalPopupMessage.ShowModalPopup("ILE Message", _msg, 1);
        }
        else
        {
            // *SteveMsg
            string _msg = "Failed to Edit the Learning Object.";
            ModalPopupMessage.ShowModalPopup("ILE Message", _msg, 2);
        }

        LoadLearningObjects(CourseID, StandardID);
    }

    protected void lnkbtnQuiz_Click(object sender, CommandEventArgs e)
    {
        //int CourseID = Convert.ToInt32(GridViewClassList.SelectedValue.ToString());
        int StandardID = Convert.ToInt16(e.CommandArgument);

        LoadQuiz(CourseID, StandardID);

        ModalPopupExtenderQuiz.Show();
    }

    private void LoadQuiz(int CourseID, int StandardID)
    {
        AssignmentsBLL assignmentsLogic = new AssignmentsBLL();
        Assignments.RelationsDataTable assignmentRelations = assignmentsLogic.GetRelationsByCourseIdAndStandardId(CourseID, StandardID);
        int assignments = 0;
        Assignments.RelationsDataTable copyAssignmentRelations = new Assignments.RelationsDataTable();

        foreach (Assignments.RelationsRow assignmentRelation in assignmentRelations)
        {
            int AssignmentID = assignmentRelation.AssignmentID;
            Assignments.AssignmentsRow assignment = assignmentsLogic.GetAssignmentByAssignmentID(AssignmentID);
            if (assignment.AssignmentType == "Quiz" && assignment.EndDateTime > DateTime.Now)
            {
                assignments++;
                copyAssignmentRelations.ImportRow(assignmentRelation);
            }
        }

        if (assignments == 0)
        {
            lblQuizMessage.Text = "There are no quizzes currently assigned for any sections of this course.<br />" +
                "Click <a href='Assignments_Quiz.aspx?Mode=Create&CourseID=" + CourseID.ToString() +
                "&StandardID=" + StandardID.ToString() + "'>here</a> to assign a new quiz.";
            RepeaterQuizSections.Visible = false;
        }
        else
        {
            lblQuizMessage.Text = "The following sections currently have quizzes assigned.<br />Click <a href='Assignments_Quiz.aspx?Mode=Edit'>here</a> to edit the assignment.<br />";
            RepeaterQuizSections.DataSource = copyAssignmentRelations.Rows;
            RepeaterQuizSections.DataBind();
            RepeaterQuizSections.Visible = true;
        }
    }

    protected void RepeaterQuizSections_ItemDataBound(object sender, RepeaterItemEventArgs e)
    {
        if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
        {
            AssignmentsBLL assignmentsLogic = new AssignmentsBLL();
            Assignments.RelationsRow row = e.Item.DataItem as Assignments.RelationsRow;

            Label Section = e.Item.FindControl("lblQuizSection") as Label;

            Assignments.AssignmentsRow assignment = assignmentsLogic.GetAssignmentByAssignmentID(row.AssignmentID);

            Section.Text = "<li><b>" + assignment.AssignmentTitle + "</b> is assigned to <b>Section " + row.SectionNumber.ToString() + "</b></li>";
        }
    }

    protected void lnkbtnEssay_Click(object sender, CommandEventArgs e)
    {
        int StandardID = Convert.ToInt16(e.CommandArgument);

        LoadEssay(CourseID, StandardID);

        ModalPopupExtenderEssay.Show();
    }

    private void LoadEssay(int CourseID, int StandardID)
    {
        AssignmentsBLL assignmentsLogic = new AssignmentsBLL();
        Assignments.RelationsDataTable assignmentRelations = assignmentsLogic.GetRelationsByCourseIdAndStandardId(CourseID, StandardID);
        int assignments = 0;
        Assignments.RelationsDataTable copyAssignmentRelations = new Assignments.RelationsDataTable();
        
        foreach (Assignments.RelationsRow assignmentRelation in assignmentRelations)
        {
            int AssignmentID = assignmentRelation.AssignmentID;
            Assignments.AssignmentsRow assignment = assignmentsLogic.GetAssignmentByAssignmentID(AssignmentID);
            if (assignment.AssignmentType == "Essay" && assignment.EndDateTime > DateTime.Now)
            {
                assignments++;
                copyAssignmentRelations.ImportRow(assignmentRelation);
            }
        }

        if (assignments == 0)
        {
            // *SteveMsg
            lblEssayMessage.Text = "There are no essays currently assigned for any sections of this course.<br />" +
                "Click <a href='Assignments_Essay.aspx?Mode=Create&CourseID=" + CourseID.ToString() +
                "&StandardID=" + StandardID.ToString() + "'>here</a> to assign a new essay assignment.";
            RepeaterEssaySections.Visible = false;
        }
        else
        {
            // *SteveMsg
            lblEssayMessage.Text = "The following sections currently have essays assigned.<br />Click <a href='Assignments_Essay.aspx?Mode=Edit'>here</a> to edit the assignment.<br />";
            RepeaterEssaySections.DataSource = copyAssignmentRelations.Rows;
            RepeaterEssaySections.DataBind();
            RepeaterEssaySections.Visible = true;
        }
    }

    protected void RepeaterEssaySections_ItemDataBound(object sender, RepeaterItemEventArgs e)
    {
        if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
        {
            AssignmentsBLL assignmentsLogic = new AssignmentsBLL();
            Assignments.RelationsRow row = e.Item.DataItem as Assignments.RelationsRow;

            Label Section = e.Item.FindControl("lblEssaySection") as Label;

            Assignments.AssignmentsRow assignment = assignmentsLogic.GetAssignmentByAssignmentID(row.AssignmentID);

            Section.Text = "<li><b>" + assignment.AssignmentTitle + "</b> is assgigned to <b>Section " + row.SectionNumber.ToString() + "</b></li>";
        }
    }

    #region Edit Course Section Standards

    protected void ddlistEditSectionSections_SelectedIndexChanged(object sender, EventArgs e)
    {
        int SectionNumber = Convert.ToInt32(((DropDownList)sender).SelectedValue);

        if (SectionNumber <= 0)
        {
            PanelSectionStandard.Visible = false;
            return;
        }
        else
        {
            PanelSectionStandard.Visible = true;
            LoadSectionStandardsConfiguration();
        }
    }

    protected void LoadSectionStandardsConfiguration()
    {
        int SectionNumber = Convert.ToInt32(ddlistEditSectionSections.SelectedValue);

        // Insert Section Info
        {
            for (int i = 1; i <= 4; i++)
            {
                if (!classStandardsLogic.IsCourseSectionStandardDuplicated(CourseID, SectionNumber, i))
                {
                    ClassStandards.Courses_StandardRow classStandard = classStandardsLogic.GetClassStandard(CourseID, i);
                    classStandardsLogic.InsertCourseSectionStandard(CourseID, SectionNumber, i, false, classStandard.IntroTitle,
                        classStandard.IntroContent, classStandard.IsIntroEnabled, classStandard.IsStandardEnabled);
                }
            }
        }

        ClassStandards.Courses_Standard_SectionRow sectionStandard = classStandardsLogic.GetClassSectionStandard(CourseID, SectionNumber, 1);
        if (sectionStandard.UseSectionInfo == true)
        {
            chkboxSectionStandardsEnabled.Checked = true;
        }
        else
        {
            chkboxSectionStandardsEnabled.Checked = false;
        }

        for (int StandardID = 1; StandardID <= 4; StandardID++)
        {
            sectionStandard = classStandardsLogic.GetClassSectionStandard(CourseID, SectionNumber, StandardID);
            LoadSectionStandardEnabled(StandardID, sectionStandard.IsStandardEnabled);
            LoadSectionStandardIntroduction(StandardID, sectionStandard);
            LoadSectionLearningObjects(CourseID, SectionNumber, StandardID);
        }
    }

    private void LoadSectionStandardEnabled(int StandardID, bool IsStandardEnabled)
    {
        if (IsStandardEnabled)
        {
            switch (StandardID)
            {
                case 1:
                    chkboxSectionStandardEnabledNeeded.Checked = true;
                    break;
                case 2:
                    chkboxSectionStandardEnabledAccessing.Checked = true;
                    break;
                case 3:
                    chkboxSectionStandardEnabledEvaluating.Checked = true;
                    break;
                case 4:
                    chkboxSectionStandardEnabledUsing.Checked = true;
                    break;
            }
        }
    }

    private void LoadSectionStandardIntroduction(int StandardID, ClassStandards.Courses_Standard_SectionRow row)
    {
        switch (StandardID)
        {
            case 1:
                lnkbtnSectionStandardIntroductionNeeded.Text = row.IntroTitle;
                if (!row.IsIntroEnabled)
                    lnkbtnSectionStandardIntroductionNeeded.ForeColor = System.Drawing.Color.Gray;
                else
                    lnkbtnSectionStandardIntroductionNeeded.ForeColor = System.Drawing.Color.Empty;
                break;
            case 2:
                lnkbtnSectionStandardIntroductionAccessing.Text = row.IntroTitle;
                if (!row.IsIntroEnabled)
                    lnkbtnSectionStandardIntroductionAccessing.ForeColor = System.Drawing.Color.Gray;
                else
                    lnkbtnSectionStandardIntroductionAccessing.ForeColor = System.Drawing.Color.Empty;
                break;
            case 3:
                lnkbtnSectionStandardIntroductionEvaluating.Text = row.IntroTitle;
                if (!row.IsIntroEnabled)
                    lnkbtnSectionStandardIntroductionEvaluating.ForeColor = System.Drawing.Color.Gray;
                else
                    lnkbtnSectionStandardIntroductionEvaluating.ForeColor = System.Drawing.Color.Empty;
                break;
            case 4:
                lnkbtnSectionStandardIntroductionUsing.Text = row.IntroTitle;
                if (!row.IsIntroEnabled)
                    lnkbtnSectionStandardIntroductionUsing.ForeColor = System.Drawing.Color.Gray;
                else
                    lnkbtnSectionStandardIntroductionUsing.ForeColor = System.Drawing.Color.Empty;
                break;
        }
    }

    private void LoadSectionLearningObjects(int CourseID, int SectionNumber, int StandardID)
    {
        ClassStandards.Courses_LearnObj_SectionDataTable learnObjs = classStandardsLogic.GetSectionLearnObjs(CourseID, SectionNumber, StandardID);

        Repeater repeaterLearningObj = new Repeater();
        switch (StandardID)
        {
            case 1:
                repeaterLearningObj = RepeaterSectionLearningObjNeeded;
                break;
            case 2:
                repeaterLearningObj = RepeaterSectionLearningObjAccessing;
                break;
            case 3:
                repeaterLearningObj = RepeaterSectionLearningObjEvaluating;
                break;
            case 4:
                repeaterLearningObj = RepeaterSectionLearningObjUsing;
                break;
        }
        repeaterLearningObj.DataSource = learnObjs.Rows;
        repeaterLearningObj.DataBind();
    }

    protected void chkboxSectionStandardsEnabled_CheckedChanged(object sender, EventArgs e)
    {
        int SectionNumber = Convert.ToInt32(ddlistEditSectionSections.SelectedValue);

        bool UseSectionInfo = chkboxSectionStandardsEnabled.Checked;
        bool result = classStandardsLogic.UpdateUseSectionInfo(UseSectionInfo, CourseID, SectionNumber);

        if (!result)
        {
            // *SteveMsg
            string _msg = "Failed to Enable or Disable the Section Standards Setting.";
            ModalPopupMessage.ShowModalPopup("ILE Message", _msg, 2);
        }
    }

    protected void chkboxSectionStandardEnabled_CheckedChanged(object sender, EventArgs e)
    {
        //int CourseID = Convert.ToInt32(GridViewClassList.SelectedValue.ToString());
        int SectionNumber = Convert.ToInt32(ddlistEditSectionSections.SelectedValue);
        int StandardID = Convert.ToInt32(AccordionSectionStandards.SelectedIndex) + 1;

        ClassStandards.Courses_Standard_SectionRow sectionStandard = classStandardsLogic.GetClassSectionStandard(CourseID, SectionNumber, StandardID);
        classStandardsLogic.UpdateSectionStandard(sectionStandard.IntroTitle, sectionStandard.IntroContent,
            sectionStandard.IsIntroEnabled, ((CheckBox)sender).Checked, CourseID, SectionNumber, StandardID);
    }

    protected void lnkbtnSectionStandardIntroduction_Click(object sender, CommandEventArgs e)
    {
        //int CourseID = Convert.ToInt32(GridViewClassList.SelectedValue.ToString());
        int SectionNumber = Convert.ToInt32(ddlistEditSectionSections.SelectedValue);
        int StandardId = Convert.ToInt32(e.CommandArgument.ToString());

        // Standard Introduction Modal Popup related.
        switch (StandardId)
        {
            case 1:
                lblSectionStandardIntroductionTitle.Text = "Getting Started";
                break;
            case 2:
                lblSectionStandardIntroductionTitle.Text = "Finding Sources";
                break;
            case 3:
                lblSectionStandardIntroductionTitle.Text = "Evaluating Sources";
                break;
            case 4:
                lblSectionStandardIntroductionTitle.Text = "Writing with Sources";
                break;
        }

        btnSectionStandardIntroductionOk.CommandArgument = StandardId.ToString();

        ClassStandards.Courses_Standard_SectionRow sectionStandard = classStandardsLogic.GetClassSectionStandard(CourseID, SectionNumber, StandardId);
        chkboxSectionStandardIntroductionEnabled.Checked = sectionStandard.IsIntroEnabled;
        txtSectionStandardIntroductionTitle.Text = sectionStandard.IntroTitle;
        txtSectionStandardIntroduction.Value = sectionStandard.IntroContent;

        ModalPopupExtenderSectionStandardIntroduction.Show();
    }

    protected void btnSectionStandardIntroductionOk_Click(object sender, CommandEventArgs e)
    {
        //int CourseID = Convert.ToInt32(GridViewClassList.SelectedValue.ToString());
        int SectionNumber = Convert.ToInt32(ddlistEditSectionSections.SelectedValue);
        int StandardId = Convert.ToInt32(e.CommandArgument.ToString());

        string introTitle = txtSectionStandardIntroductionTitle.Text;
        string introDescription = txtSectionStandardIntroduction.Value;
        bool introEnabled = chkboxSectionStandardIntroductionEnabled.Checked;

        bool standardEnabled = classStandardsLogic.GetClassSectionStandard(CourseID, SectionNumber, StandardId).IsStandardEnabled;

        bool result = classStandardsLogic.UpdateSectionStandard(introTitle, introDescription, introEnabled, standardEnabled, CourseID, SectionNumber, StandardId);

        if (result)
        {
            LoadSectionStandardsConfiguration();
            // *SteveMsg
            string _msg = "Standard Introduction has been Successfully Updated." + introTitle + " 77 " + introDescription;
            ModalPopupMessage.ShowModalPopup("ILE Message", _msg, 1);
        }
        else
        {
            // *SteveMsg
            string _msg = "Failed to Update Standard Introduction.";
            ModalPopupMessage.ShowModalPopup("ILE Message", _msg, 2);
        }
    }

    protected void RepeaterSectionLearningObj_ItemDataBound(object sender, RepeaterItemEventArgs e)
    {
        if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
        {
            ClassStandards.Courses_LearnObj_SectionRow row = e.Item.DataItem as ClassStandards.Courses_LearnObj_SectionRow;

            ImageButton SortUp = e.Item.FindControl("imgbtnSectionSortUp") as ImageButton;
            ImageButton SortDown = e.Item.FindControl("imgbtnSectionSortDown") as ImageButton;
            LinkButton Title = e.Item.FindControl("lnkbtnSectionEditLearningObj") as LinkButton;
            LinkButton Remove = e.Item.FindControl("lnkbtnSectionLearningObjRemove") as LinkButton;

            // Passes Two Command Arguments. StandardID;LearnObjID
            SortUp.CommandArgument = SortDown.CommandArgument = Remove.CommandArgument = row.StandardID.ToString() + ";" + row.LearnObjID.ToString();

            if (!row.UseAlterInfo)
            {
                LearnObjsBLL learnObjsLogic = new LearnObjsBLL();
                LearnObjs.LearnObjsRow learnObj = learnObjsLogic.GetLearnObj(row.LearnObjID);
                Title.Text = learnObj.Title.ToString();
            }
            else
            {
                Title.Text = row.AlterTitle;
            }

            int StandardID = Convert.ToInt16(Title.CommandArgument);

            switch (StandardID)
            {
                case 1:
                    Title.CommandArgument = "1;" + row.LearnObjID.ToString();
                    break;
                case 2:
                    Title.CommandArgument = "2;" + row.LearnObjID.ToString();
                    break;
                case 3:
                    Title.CommandArgument = "3;" + row.LearnObjID.ToString();
                    break;
                case 4:
                    Title.CommandArgument = "4;" + row.LearnObjID.ToString();
                    break;
                default:
                    break;
            }

            if (!row.IsEnabled)
            {
                Title.ForeColor = System.Drawing.Color.Gray;
            }
        }
    }

    protected void imgbtnSectionSort_Click(object sender, CommandEventArgs e)
    {
        //int CourseID = Convert.ToInt32(GridViewClassList.SelectedValue.ToString());
        int SectionNumber = Convert.ToInt32(ddlistEditSectionSections.SelectedValue);

        string[] commandArguments = new string[2];
        commandArguments = e.CommandArgument.ToString().Split(';');
        int StandardID = Convert.ToInt32(commandArguments[0]);
        int LearnObjID = Convert.ToInt32(commandArguments[1]);

        switch (e.CommandName)
        {
            case "SortUp":
                classStandardsLogic.DecreaseClassSectionLearnObjSortOrder(CourseID, SectionNumber, StandardID, LearnObjID);
                break;
            case "SortDown":
                classStandardsLogic.IncreaseClassSectionLearnObjSortOrder(CourseID, SectionNumber, StandardID, LearnObjID);
                break;
            default:
                break;
        }

        LoadSectionLearningObjects(CourseID, SectionNumber, StandardID);
    }

    protected void lnkbtnSectionLearningObjRemove_Click(object sender, CommandEventArgs e)
    {
        //int CourseID = Convert.ToInt32(GridViewClassList.SelectedValue.ToString());
        int SectionNumber = Convert.ToInt32(ddlistEditSectionSections.SelectedValue);

        string[] commandArguments = new string[2];
        commandArguments = e.CommandArgument.ToString().Split(';');
        int StandardID = Convert.ToInt32(commandArguments[0]);
        int LearnObjID = Convert.ToInt32(commandArguments[1]);

        bool result = classStandardsLogic.DeleteClassSectionLearnObj(CourseID, SectionNumber, StandardID, LearnObjID);

        if (result)
        {
            LoadSectionStandardsConfiguration();
            // *SteveMsg
            string _msg = "The Learning Object has been Successfully Deleted.";
            ModalPopupMessage.ShowModalPopup("ILE Message", _msg, 1);
        }
        else
        {
            // *SteveMsg
            string _msg = "Failed to Delete the Learning Object.";
            ModalPopupMessage.ShowModalPopup("ILE Message", _msg, 2);
        }
    }

    /// <summary>
    /// Edit Existing Learning Object
    /// </summary>
    protected void lnkbtnSectionEditLearningObj_Click(object sender, CommandEventArgs e)
    {
        //int CourseID = Convert.ToInt32(GridViewClassList.SelectedValue.ToString());
        int SectionNumber = Convert.ToInt32(ddlistEditSectionSections.SelectedValue);

        string[] commandArguments = new string[2];
        commandArguments = e.CommandArgument.ToString().Split(';');
        int StandardID = Convert.ToInt32(commandArguments[0]);
        int LearnObjID = Convert.ToInt32(commandArguments[1]);

        ClassStandards.Courses_LearnObj_SectionRow sectionLearnObj = classStandardsLogic.GetClassSectionLearnObj(CourseID, SectionNumber, StandardID, LearnObjID);

        chkboxSectionEditUseAlternativeLearningObjDirections.Checked = sectionLearnObj.UseAlterInfo;
        if (sectionLearnObj.UseAlterInfo)
        {
            lblSectionEditDefaultLearningObjDirections.Visible = false;
            txtSectionEditAlterLearningObjDirections.Visible = true;
            txtSectionEditAlterLearningObjTitle.Enabled = true;

            txtSectionEditAlterLearningObjTitle.Text = sectionLearnObj.AlterTitle;
            txtSectionEditAlterLearningObjDirections.Value = sectionLearnObj.AlterDescription;
            chkboxSectionEditLearningObjEnabled.Checked = sectionLearnObj.IsEnabled;
        }
        else
        {
            lblSectionEditDefaultLearningObjDirections.Visible = true;
            txtSectionEditAlterLearningObjDirections.Visible = false;
            txtSectionEditAlterLearningObjTitle.Enabled = false;

            LearnObjsBLL learnObjsLogic = new LearnObjsBLL();
            LearnObjs.LearnObjsRow learnObj = learnObjsLogic.GetLearnObj(LearnObjID);

            txtSectionEditAlterLearningObjTitle.Text = learnObj.Title;
            lblSectionEditDefaultLearningObjDirections.Text = learnObj.Instruction;
            chkboxSectionEditLearningObjEnabled.Checked = sectionLearnObj.IsEnabled;
        }

        btnSectionEditLearningObjOk.CommandArgument = e.CommandArgument.ToString();

        ModalPopupExtenderSectionEditLearningObj.Show();
    }

    protected void lnkbtnSectionLearningObjAdd_Click(object sender, CommandEventArgs e)
    {
        //int CourseID = Convert.ToInt32(GridViewClassList.SelectedValue.ToString());
        int SectionNumber = Convert.ToInt32(ddlistEditSectionSections.SelectedValue);
        int StandardId = Convert.ToInt32(e.CommandArgument.ToString());
        
        btnSectionAddLearningObjOk.CommandArgument = StandardId.ToString();

        switch (StandardId)
        {
            case 1:
                lblSectionAddLearningObjTitle.Text = "Getting Started";
                break;
            case 2:
                lblSectionAddLearningObjTitle.Text = "Finding Sources";
                break;
            case 3:
                lblSectionAddLearningObjTitle.Text = "Evaluating Sources";
                break;
            case 4:
                lblSectionAddLearningObjTitle.Text = "Writing with Sources";
                break;
        }

        // Fill ddlistSectionAvailableLearningObj
        LearnObjsBLL learnObjsLogic = new LearnObjsBLL();
        LearnObjs.RelationsLearnObjStandardDataTable learnObjs = learnObjsLogic.GetLearnObjsByStandardID(StandardId);
        List<LearnObjs.RelationsLearnObjStandardRow> sortedLearnObjs = new List<LearnObjs.RelationsLearnObjStandardRow>();
        ddlistSectionAvailableLearningObj.Items.Clear();

        ClassStandards.Courses_LearnObj_SectionDataTable existingLearnObjs = classStandardsLogic.GetClassSectionLearnObjs(CourseID, SectionNumber, StandardId);

        int tempIterator = (learnObjs.Count - 1);

        for (int i = tempIterator; i >= 0; i--)
        {
            foreach (ClassStandards.Courses_LearnObj_SectionRow existingLearnObj in existingLearnObjs)
            {
                if (learnObjs[i].LearnObjID == existingLearnObj.LearnObjID)
                {
                    learnObjs.Rows.RemoveAt(i);
                    break;
                }
            }
        }

        if (learnObjs.Count == 0)
        {
            ListItem defaultItem = new ListItem("--- No Learning Object Available ---", "0");
            ddlistSectionAvailableLearningObj.Items.Add(defaultItem);
        }
        else
        {
            ListItem defaultItem = new ListItem("--- Select Learning Object ---", "0");
            ddlistSectionAvailableLearningObj.Items.Add(defaultItem);
            sortedLearnObjs = learnObjs.OrderBy(p => p.Title).ToList();
        }


        foreach (LearnObjs.RelationsLearnObjStandardRow learnObj in sortedLearnObjs)
        {
            ListItem newItem = new ListItem(learnObj.Title, learnObj.LearnObjID.ToString());
            ddlistSectionAvailableLearningObj.Items.Add(newItem);
        }

        txtSectionAddAlterLearningObjDirections.Visible = false;
        lblSectionAddDefaultLearningObjDirections.Visible = true;
        lblSectionAddDefaultLearningObjDirections.Text = "<span style='width: 100%; text-align: center; font-weight: bold;'>Please Select a Learning Object.</span>";

        ModalPopupExtenderSectionAddLearningObj.Show();
    }

    protected void lnkbtnSectionCopyCourseInfo_Click(object sender, CommandEventArgs e)
    {
        int StandardID = Convert.ToInt32(e.CommandArgument.ToString());
        int SectionNumber = Convert.ToInt32(ddlistEditSectionSections.SelectedValue);
        ClassStandards.Courses_LearnObjDataTable courseLearnObjs = classStandardsLogic.GetClassLearnObjs(CourseID, StandardID);

        bool result = true;

        foreach(ClassStandards.Courses_LearnObjRow courseLearnObj in courseLearnObjs)
        {
            try
            {
                classStandardsLogic.InsertSectionLearnObj(CourseID, SectionNumber, StandardID, courseLearnObj.LearnObjID, courseLearnObj.UseAlterInfo, courseLearnObj.AlterTitle, courseLearnObj.AlterDescription, courseLearnObj.IsEnabled);
            }
            catch
            {
                result = false;
            }
        }

        LoadSectionLearningObjects(CourseID, SectionNumber, StandardID);

        if (result)
        {
            ModalPopupMessage.ShowModalPopup("ILE Message", "Copied All Course Learning Objects", 1);
        }
        else
        {
            ModalPopupMessage.ShowModalPopup("ILE Message", "Copied Some Course Learning Objects", 1);
        }
    }

    protected void ddlistSectionAvailableLearningObj_SelectedIndexChanged(object sender, EventArgs e)
    {
        LearnObjsBLL learnObjsLogic = new LearnObjsBLL();

        if (ddlistSectionAvailableLearningObj.SelectedIndex != 0)
        {
            int LearnObjID = Convert.ToInt32(ddlistSectionAvailableLearningObj.SelectedValue);
            LearnObjs.LearnObjsRow learnObj = learnObjsLogic.GetLearnObj(LearnObjID);
            txtSectionAddAlterLearningObjTitle.Text = learnObj.Title;
            lblSectionAddDefaultLearningObjDirections.Text = learnObj.Instruction;
        }
        else
        {
            chkboxSectionAddUseAlternativeLearningObjDirections.Checked = false;
            txtSectionAddAlterLearningObjTitle.Text = String.Empty;
            txtSectionAddAlterLearningObjDirections.Value = String.Empty;
            txtSectionAddAlterLearningObjTitle.Enabled = false;
            txtSectionAddAlterLearningObjDirections.Visible = false;
            lblSectionAddDefaultLearningObjDirections.Visible = true;
            lblSectionAddDefaultLearningObjDirections.Text = "<span style='width: 100%; text-align: center; font-weight: bold;'>Please Select a Learning Object.</span>";
        }

        ModalPopupExtenderSectionAddLearningObj.Show();
    }

    protected void chkboxSectionAddUseAlternativeLearningObjDirections_CheckedChanged(object sender, EventArgs e)
    {
        if (chkboxSectionAddUseAlternativeLearningObjDirections.Checked)
        {
            txtSectionAddAlterLearningObjTitle.Enabled = true;
            txtSectionAddAlterLearningObjDirections.Visible = true;
            lblSectionAddDefaultLearningObjDirections.Visible = false;
        }
        else
        {
            txtSectionAddAlterLearningObjTitle.Enabled = false;
            txtSectionAddAlterLearningObjDirections.Visible = false;
            lblSectionAddDefaultLearningObjDirections.Visible = true;
        }

        ModalPopupExtenderSectionAddLearningObj.Show();
    }

    protected void btnSectionAddLearningObjOk_Click(object sender, CommandEventArgs e)
    {
        if (ddlistSectionAvailableLearningObj.SelectedIndex != 0)
        {
            //int CourseID = Convert.ToInt32(GridViewClassList.SelectedValue.ToString());
            int SectionNumber = Convert.ToInt32(ddlistEditSectionSections.SelectedValue);
            int StandardId = Convert.ToInt32(e.CommandArgument.ToString());
            int LearnObjID = Convert.ToInt32(ddlistSectionAvailableLearningObj.SelectedValue);
            string AlterTitle = txtSectionAddAlterLearningObjTitle.Text;
            string AlterDirections = txtSectionAddAlterLearningObjDirections.Value;
            bool IsEnabled = chkboxSectionAddLearningObjEnabled.Checked;

            bool result = false;

            if (!classStandardsLogic.IsSectionLearnObjDuplicated(CourseID, SectionNumber, StandardId, LearnObjID))
            {
                if (chkboxSectionAddUseAlternativeLearningObjDirections.Checked)
                {
                    result = classStandardsLogic.InsertSectionLearnObj(CourseID, SectionNumber, StandardId, LearnObjID, true, AlterTitle, AlterDirections, IsEnabled);
                }
                else
                {
                    result = classStandardsLogic.InsertSectionLearnObj(CourseID, SectionNumber, StandardId, LearnObjID, false, String.Empty, String.Empty, IsEnabled);
                }

                if (result)
                {
                    LoadSectionLearningObjects(CourseID, SectionNumber, StandardId);
                    ResetSectionAddNewLearningObj();
                    // *SteveMsg
                    string _msg = "Successfully Added a New Learning Object.";
                    ModalPopupMessage.ShowModalPopup("ILE Message", _msg, 1);
                }
                else
                {
                    ResetSectionAddNewLearningObj();
                    // *SteveMsg
                    string _msg = "Failed to Add a New Learning Object.";
                    ModalPopupMessage.ShowModalPopup("ILE Message", _msg, 2);
                }
            }
            else
            {
                ResetSectionAddNewLearningObj();
                // *SteveMsg
                string _msg = "The Learning Object already exists.";
                ModalPopupMessage.ShowModalPopup("ILE Message", _msg, 2);
            }
        }
    }

    protected void ResetSectionAddNewLearningObj()
    {
        chkboxSectionAddUseAlternativeLearningObjDirections.Checked = false;
        txtSectionAddAlterLearningObjTitle.Text = String.Empty;
        txtSectionAddAlterLearningObjTitle.Enabled = false;
        txtSectionAddAlterLearningObjDirections.Visible = false;
        lblSectionAddDefaultLearningObjDirections.Text = "<span style='width: 100%; text-align: center; font-weight: bold;'>Please Select a Learning Object.</span>";
        lblSectionAddDefaultLearningObjDirections.Visible = true;
        chkboxSectionAddLearningObjEnabled.Checked = true;
    }

    protected void chkboxSectionEditUseAlternativeLearningObjDirections_CheckedChanged(object sender, EventArgs e)
    {
        //int CourseID = Convert.ToInt32(GridViewClassList.SelectedValue.ToString());
        int SectionNumber = Convert.ToInt32(ddlistEditSectionSections.SelectedValue);

        string[] commandArguments = new string[2];
        commandArguments = btnSectionEditLearningObjOk.CommandArgument.ToString().Split(';');
        int StandardID = Convert.ToInt32(commandArguments[0]);
        int LearnObjID = Convert.ToInt32(commandArguments[1]);

        ClassStandards.Courses_LearnObj_SectionRow sectionLearnObj = classStandardsLogic.GetClassSectionLearnObj(CourseID, SectionNumber, StandardID, LearnObjID);

        if (chkboxSectionEditUseAlternativeLearningObjDirections.Checked)
        {
            lblSectionEditDefaultLearningObjDirections.Visible = false;
            txtSectionEditAlterLearningObjDirections.Visible = true;
            txtSectionEditAlterLearningObjTitle.Enabled = true;
        }
        else
        {
            lblSectionEditDefaultLearningObjDirections.Visible = true;
            txtSectionEditAlterLearningObjDirections.Value = String.Empty;
            txtSectionEditAlterLearningObjDirections.Visible = false;
            txtSectionEditAlterLearningObjTitle.Enabled = false;

            LearnObjsBLL learnObjsLogic = new LearnObjsBLL();
            LearnObjs.LearnObjsRow learnObj = learnObjsLogic.GetLearnObj(LearnObjID);

            txtSectionEditAlterLearningObjTitle.Text = learnObj.Title;
        }  

        ModalPopupExtenderSectionEditLearningObj.Show();
    }

    protected void btnSectionEditLearningObjOk_Click(object sender, CommandEventArgs e)
    {
        //int CourseID = Convert.ToInt32(GridViewClassList.SelectedValue.ToString());
        int SectionNumber = Convert.ToInt32(ddlistEditSectionSections.SelectedValue);

        string[] commandArguments = new string[2];
        commandArguments = e.CommandArgument.ToString().Split(';');
        int StandardID = Convert.ToInt32(commandArguments[0]);
        int LearnObjID = Convert.ToInt32(commandArguments[1]);

        bool UseAlterInfo = chkboxSectionEditUseAlternativeLearningObjDirections.Checked;

        string AlterTitle = String.Empty;
        string AlterDirections = String.Empty;

        if (UseAlterInfo)
        {
            AlterTitle = txtSectionEditAlterLearningObjTitle.Text;
            AlterDirections = txtSectionEditAlterLearningObjDirections.Value;
        }
        else
        {
            AlterTitle = String.Empty;
            AlterDirections = String.Empty;
        }

        bool IsEnabled = chkboxSectionEditLearningObjEnabled.Checked;

        bool result = classStandardsLogic.UpdateSectionLearnObj(UseAlterInfo, AlterTitle, AlterDirections, IsEnabled, CourseID, SectionNumber, StandardID, LearnObjID);

        if (result)
        {
            // *SteveMsg
            string _msg = "Successfully Edited the Learning Object.";
            ModalPopupMessage.ShowModalPopup("ILE Message", _msg, 1);
        }
        else
        {
            // *SteveMsg
            string _msg = "Failed to Edit the Learning Object.";
            ModalPopupMessage.ShowModalPopup("ILE Message", _msg, 2);
        }

        LoadSectionLearningObjects(CourseID, SectionNumber, StandardID);
    }

    protected void lnkbtnSectionQuiz_Click(object sender, CommandEventArgs e)
    {
        //int CourseID = Convert.ToInt32(GridViewClassList.SelectedValue.ToString());
        int SectionNumber = Convert.ToInt32(ddlistEditSectionSections.SelectedValue);
        int StandardID = Convert.ToInt16(e.CommandArgument);

        LoadSectionQuiz(CourseID, SectionNumber, StandardID);

        ModalPopupExtenderSectionQuiz.Show();
    }

    private void LoadSectionQuiz(int CourseID, int SectionNumber, int StandardID)
    {
        AssignmentsBLL assignmentsLogic = new AssignmentsBLL();
        Assignments.RelationsDataTable assignmentRelations = assignmentsLogic.GetRelationsByCourseIdAndSectionNumberAndStandardId(CourseID, SectionNumber, StandardID);
        Assignments.RelationsDataTable copyAssignmentRelations = new Assignments.RelationsDataTable();

        int assignments = 0;

        foreach (Assignments.RelationsRow assignmentRelation in assignmentRelations)
        {
            int AssignmentID = assignmentRelation.AssignmentID;
            Assignments.AssignmentsRow assignment = assignmentsLogic.GetAssignmentByAssignmentID(AssignmentID);
            if (assignment.AssignmentType == "Quiz" && assignment.EndDateTime > DateTime.Now)
            {
                assignments++;
                copyAssignmentRelations.ImportRow(assignmentRelation);
            }
        }

        if (assignments == 0)
        {
            // *SteveMsg
            lblSectionQuizMessage.Text = "There is no upcoming or(and) ongoing quiz assignment for this section.<br />" +
                "Click <a href='Assignments_Quiz.aspx?Mode=Create&CourseID=" + CourseID.ToString() +
                "&StandardID=" + StandardID.ToString() + "'>here</a> to assign a new quiz.";
            RepeaterSectionQuizSections.Visible = false;
        }
        else
        {
            // *SteveMsg
            lblSectionQuizMessage.Text = "Currently, there are upcoming and (or) ongoing quiz assignments for this section.<br />";
            RepeaterSectionQuizSections.DataSource = copyAssignmentRelations.Rows;
            RepeaterSectionQuizSections.DataBind();
            RepeaterSectionQuizSections.Visible = true;
        }
    }

    protected void RepeaterSectionQuizSections_ItemDataBound(object sender, RepeaterItemEventArgs e)
    {
        if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
        {
            AssignmentsBLL assignmentsLogic = new AssignmentsBLL();
            Assignments.RelationsRow row = e.Item.DataItem as Assignments.RelationsRow;

            Label Section = e.Item.FindControl("lblQuizSection") as Label;

            Section.Text = row.AssignmentID.ToString();
        }
    }

    protected void lnkbtnSectionEssay_Click(object sender, CommandEventArgs e)
    {
        int SectionNumber = Convert.ToInt32(ddlistEditSectionSections.SelectedValue);
        int StandardID = Convert.ToInt16(e.CommandArgument);

        LoadSectionEssay(CourseID, SectionNumber, StandardID);

        ModalPopupExtenderSectionEssay.Show();
    }

    private void LoadSectionEssay(int CourseID, int SectionNumber, int StandardID)
    {
        AssignmentsBLL assignmentsLogic = new AssignmentsBLL();
        Assignments.RelationsDataTable assignmentRelations = assignmentsLogic.GetRelationsByCourseIdAndSectionNumberAndStandardId(CourseID, SectionNumber, StandardID);
        int assignments = 0;
        Assignments.RelationsDataTable copyAssignmentRelations = new Assignments.RelationsDataTable();

        foreach (Assignments.RelationsRow assignmentRelation in assignmentRelations)
        {
            int AssignmentID = assignmentRelation.AssignmentID;
            Assignments.AssignmentsRow assignment = assignmentsLogic.GetAssignmentByAssignmentID(AssignmentID);
            if (assignment.AssignmentType == "Essay" && assignment.EndDateTime > DateTime.Now)
            {
                assignments++;
                copyAssignmentRelations.ImportRow(assignmentRelation);
            }
        }

        if (assignments == 0)
        {
            lblSectionQuizMessage.Text = "There is no Essay assignment assigned to this section.<br />" +
                "Click <a href='Assignments_Essay.aspx?Mode=Create&CourseID=" + CourseID.ToString() +
                "&StandardID=" + StandardID.ToString() + "'>here</a> to assign a new essay assignment.";
            RepeaterSectionQuizSections.Visible = false;
        }
        else
        {
            lblSectionQuizMessage.Text = "Currently, there are essay assignments assigned to this section.<br />";
            RepeaterSectionEssaySections.DataSource = copyAssignmentRelations.Rows;
            RepeaterSectionEssaySections.DataBind();
            RepeaterSectionEssaySections.Visible = true;
        }
    }

    protected void RepeaterSectionEssaySections_ItemDataBound(object sender, RepeaterItemEventArgs e)
    {
        if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
        {
            AssignmentsBLL assignmentsLogic = new AssignmentsBLL();
            Assignments.RelationsRow row = e.Item.DataItem as Assignments.RelationsRow;

            Label Section = e.Item.FindControl("lblEssaySection") as Label;

            Section.Text = row.AssignmentID.ToString();
        }
    }

    #endregion
}
